package summary;

/**
 * @Author: 海琳琦
 * @Date: 2022/3/13 15:13
 * https://leetcode-cn.com/problems/distinct-subsequences/
 */
public class Title115 {

    /**
     * dp[i][j]表示以下标i-1结尾的A中出现以下标j-1结尾的B的子序列个数
     * 递推公式：
     *          当s.charAt(i-1) == t.charAt(j-1)
     *                            s[i-1]用作匹配时的个数， dp[i-1][j-1]
     *                            s[i-1]不用作匹配时的个数,dp[i-1][j]
     *          当s.charAt(i-1) != t.charAt(j-1)
     *                            dp[i-1][j]
     * @param s
     * @param t
     * @return
     */
    public static int numDistinct(String s, String t) {
        int[][] dp = new int[t.length() + 1][s.length() + 1];
        //初始化dp[i][0] dp[0][j]
        for (int i = 0; i < t.length(); i++) {
            dp[i][0] = 0;
        }
        for (int j = 0; j < s.length(); j++) {
            dp[0][j] = 1;
        }
        for (int i = 1; i <= t.length(); i++) {
            for (int j = 1; j <=s.length() ; j++) {
                if (t.charAt(i - 1) == s.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1];
                } else {
                    dp[i][j] = dp[i][j - 1];
                }
            }
        }
//        for (int i = 0; i <= t.length(); i++) {
//            for (int j = 0; j <= s.length() ; j++) {
//                System.out.print(dp[i][j]+" ");
//            }
//            System.out.println();
//        }
        return dp[t.length()][s.length()];
    }

    public int numDistinct2(String s, String t) {
        int n = s.length();
        int m = t.length();
        //dp[i][j]表示i-1,j-1时，t在s中出现的次数
        int[][] dp = new int[n + 1][m + 1];
        for (int i = 0; i <= n; i++) {
            dp[i][0] = 1;
        }
        for (int j = 1; j <= m; j++) {
            dp[0][j] = 0;
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (s.charAt(i - 1) == t.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j];
                }else{
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }
        return dp[n][m];
    }







    public int minDistance3(String word1, String word2) {
        int n = word1.length();
        int m = word2.length();
        //dp[i][j]表示i-1,j-1，word1和word2相同的最小步数
        int[][] dp = new int[n + 1][m + 1];
        for (int i = 0; i <= n; i++) {
            dp[i][0] = i;
        }
        for (int i = 0; i <= m; i++) {
            dp[0][i] = m;
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (word1.charAt(i - 1) == word2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1];
                }else{
                    //只能删除
                    dp[i][j] = Math.min(dp[i - 1][j - 1] + 2, Math.min(dp[i - 1][j], dp[i][j - 1]) + 1);
                }
            }
        }
        return dp[n][m];
    }












    public static void main(String[] args) {
        numDistinct("rabbbit"
                ,"rabbit");
    }
}
